package ysq.xyg.base.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.model.po.SysMenu;
import ysq.xyg.sys.model.po.SysRoleMenu;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysRoleMenuVo;
import ysq.xyg.sys.model.vo.SysUserVo;
import ysq.xyg.sys.service.SysRoleMenuService;
import ysq.xyg.sys.service.SysUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 通用部分-用户controller层
 *
 * @author shengQiang yu
 * @date 2018-03-18 15:42
 */
@RestController
public class CommonController extends BaseController {
    private static Logger log= LoggerFactory.getLogger(CommonController.class);
    @Resource
    private SysRoleMenuService sysRoleMenuService;
    @Resource
    private SysUserService sysUserService;
    /**
     * 登录页面（默认是首页）
     * */
    @RequestMapping(value="/managerLogin")
    public ModelAndView login(HttpServletRequest request,HttpServletResponse response){
        SysUserVo sysUserVo=new SysUserVo();
        sysUserVo.setLoginName(BaseController.CookiesAcceptObject(request, BaseConstant.COOKIE_LOGINNAME));
        sysUserVo.setPassword(BaseController.CookiesAcceptObject(request, BaseConstant.COOKIE_PASSWORD));
        if(StrUtil.isNotEmpty(sysUserVo.getLoginName())&&StrUtil.isNotEmpty(sysUserVo.getPassword())){
            sysUserVo.setAutoLogin("2");
        }else{
            sysUserVo.setAutoLogin("1");
        }
        return new ModelAndView("/login").addObject("sysUserVo",sysUserVo);
    }
    /**
     * 登录进入默认页面
     * */
    @RequestMapping(value="/main")
    public ModelAndView main(HttpServletRequest request,HttpServletResponse response){
        SysUser currentUser=acceptCurrentUser(request);
        Date newDate=new Date();
        List<String> dataList=new ArrayList<String>();
        List<Integer> memberList=new ArrayList<Integer>();
        List<Double> recordList=new ArrayList<Double>();
        /*EntryFormOne entryFormOne=new EntryFormOne();
        entryFormOne.setStatus("2");//已交费
        Double allOrderCount=entryFormOneService.selectAllCount(entryFormOne);
        entryFormOne.setStartDate(DateUtil.DateToStr(newDate, DateUtil.DATE_PATTERN)+" 00:00:00");
        Double currentOrderCount=entryFormOneService.selectAllCount(entryFormOne);

        //获取最近7天
        for(int i=0;i<7;i++){
            dataList.add(DateUtil.DateToStr(DateUtil.getDateOfDate(newDate,i), DateUtil.DATE_PATTERN));
            Member memberEntity=new Member();
            memberEntity.setStartDate(dataList.get(i)+" 00:00:00");
            memberEntity.setEndDate(dataList.get(i)+" 23:59:59");
            int  mSize= memberService.selectMemberCount(memberEntity);

            EntryFormOne entryFormOnes=new EntryFormOne();
            entryFormOnes.setStatus("2");//已交费
            entryFormOnes.setStartDate(dataList.get(i)+" 00:00:00");
            entryFormOnes.setEndDate(dataList.get(i)+" 23:59:59");
            Double pSize=entryFormOneService.selectAllCount(entryFormOnes);
            memberList.add(mSize);
            recordList.add(pSize);
        }*/

        return new ModelAndView("/sys/baseMain").addObject("currentUser",currentUser)
               /* .addObject("allOrderCount",allOrderCount)
                .addObject("currentOrderCount",currentOrderCount)*/
                .addObject("dataList", JSONArray.toJSONString(dataList))
                .addObject("memberList",JSONArray.toJSONString(memberList))
                .addObject("recordList",JSONArray.toJSONString(recordList))
                ;
    }
    /**
     *后端登录平台
     */
    @RequestMapping(value="/webLogin",method= RequestMethod.POST)
    public ReturnEntity webLogin(HttpServletRequest request, HttpServletResponse response, String loginName, String password,String autoLogin){
        return sysUserService.WebLogin(request,response,loginName, password,autoLogin);
    }
    /**
     * 个人中心页面
     * */
    @RequestMapping(value="/index")
    public ModelAndView Index(HttpServletRequest request,HttpServletResponse response){
        SysUser sysUser=acceptCurrentUser(request);
        //获取一级目录
        SysRoleMenuVo sysRoleMenuVo=new SysRoleMenuVo(sysUser.getRoleId(),BaseConstant.SYS_MENU_TYPE_DIC,BaseConstant.SYS_MENU_GRADE_ONE);
        List<SysRoleMenu> sysRoleMenuList=sysRoleMenuService.listLeft(sysRoleMenuVo);
        String url="";
        if(sysRoleMenuList!=null&&sysRoleMenuList.size()>0){
            SysRoleMenuVo sysRoleTwoMenuVo=new SysRoleMenuVo(sysUser.getRoleId());
            sysRoleTwoMenuVo.setParentId(sysRoleMenuList.get(0).getMenuId());
            List<SysRoleMenu> sysRoleTwoMenuList=sysRoleMenuService.listLeft(sysRoleTwoMenuVo);
            if(sysRoleTwoMenuList!=null&&sysRoleTwoMenuList.size()>0){
                url=sysRoleTwoMenuList.get(0).getMenuUrl();
            }
        }
        ModelAndView modelAndView=new ModelAndView("/index");
        modelAndView.addObject("sysRoleMenuList",sysRoleMenuList);
        modelAndView.addObject("currentRoleMenu",sysRoleMenuList.get(0));
        modelAndView.addObject("url",url);
        modelAndView.addObject("currentUser",sysUser);
        return modelAndView;
    }
    /**
     *@Author: YuShengQiang
     *@Date: 2018-11-22 9:59
     *@Description: 后端登录退出
     */
    @RequestMapping(value="/webExit",method= RequestMethod.GET)
    public ReturnEntity webExit(HttpServletRequest request){
        return sysUserService.webExit(request);
    }
    /**
     * 导航菜单
     * @param departId 部门id
     * @param menuId 菜单id
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = "/menuList")
    public @ResponseBody
    JSONObject menuList(String  departId, String menuId) throws UnsupportedEncodingException {
        //菜单查询
        SysMenu sysMenu=new SysMenu();
        sysMenu.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        sysMenu.setMenuType(BaseConstant.SYS_MENU_TYPE_MENU);
        sysMenu.setParentId(menuId);
        JSONObject jsonObject=new JSONObject();
        List<SysRoleMenu> sysRoleMenuList=sysRoleMenuService.listLeft(new SysRoleMenuVo());
        /*sysMenu.setD(departId);
        List<BaseMenu> baseMenuList=baseMenuService.selectEntityByLeft(baseMenu);

        JSONArray rows = new JSONArray();
        for (BaseMenu baseMenu1:baseMenuList){
            JSONObject row=new JSONObject();
            row.put("title",baseMenu1.getMenuname());
            row.put("icon",baseMenu1.getMenusign());
            row.put("href",baseMenu1.getMenuurl());
            row.put("spread",false);
            rows.add(row);
        }*/
        //jsonObject.put("rows",rows);
        return jsonObject;
    }
}
